package com.site.common;

import java.sql.*;

/**
 * 操作数据库的公共类
 */
public class DbHelper {
    /*
    * 1.加载驱动
    * 2.建立连接
    * 3.创建操作数据的对象
    * 4.执行sql命令
    * 5.释放资源
    * */

    private Connection con;
    private Statement sta;
    public DbHelper() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
//        con= DriverManager.getConnection("连接数据库地址","账号","密码");
        String url="jdbc:mysql://localhost:3306/website",user="root",pwd="20011004";
        con=DriverManager.getConnection(url,user,pwd);
    }

    /**
     * 查询
     * @param sql 查询的sql语句
     * @return Result结果集
     * @throws SQLException
     */
    public ResultSet query(String sql) throws SQLException {
        sta=con.createStatement();
        return sta.executeQuery(sql);
    }

    /**
     * 释放资源
     * @throws SQLException
     */
    public void close() throws SQLException {
        sta.close();
        con.close();
    }
}
